<script type="text/javascript">
	function AttenDanceManagerCtrl($scope, $http, modalService, CtrlUtil, $element) {
		var ctrl = this
		CtrlUtil.build(ctrl, ':root/course/queryCourse')
		var pages = 'Q,L,D'
		ctrl.time = $scope.$server.time
		var Q = {}
		Q.onQuerySuccess = function(r) {
			var ids = []
			angular.forEach(r.content, function(v) {
				ids.push(v.id)
				var status = v.status
				if (status == 1 || ctrl.time >= v.kcsj) {
					v.statusString = '已过期'
					v.status = 1
				} else {
					v.statusString = '即将开课'
					v.status = 0
				}
			})
			$http.post(':root/course/getCourseApplyCount', {
				ids : ids
			}).success(function(R) {
				CtrlUtil.addProperty(r.content, R, 'applyCount')
			})
			$http.post(':root/course/queryAttend', {
				courseId : ids
			}).success(function(R) {
				var map = {};
				angular.forEach(R, function(v) {
					var c = map[v[0]] = map[v[0]] || {
						id : v[0],
						attend : 0,
						leave : 0,
						absence : 0
					}
					if (v[1] == 0) {
						c.attend++
					}
					if (v[1] == 1) {
						c.leave++
					}
					if (v[1] == 2) {
						c.absence++
					}
				})
				var ext = []
				angular.forEach(map, function(v) {
					ext.push(v)
				})
				CtrlUtil.addProperty(r.content, ext, 'checking')
			})
		}
		CtrlUtil.build(Q, ':root/course/queryCourse', ctrl)
		Q.audit = function(item) {
			ctrl.currentCourse = item
			ctrl.courseQueryParam = item.id;
			ctrl.changeCtrl('L')
		}
		Q.detail = function(item) {
			ctrl.currentCourse = item
			ctrl.courseQueryParam = item.id;
			ctrl.changeCtrl('D')
		}
		Q.import = function() {
			var content = $element.find('[import]').clone()
			var modal = modalService.show({
				title : '考勤导入',
				content : content,
				upload : function() {
					content.find('form').ajaxForm().ajaxSubmit({
						url : require.toUrl('root/course/importAttendance'),
						success : function(r) {
							if (r == '') {
								modalService.succ('导入成功', 1500)
							} else {
								modalService.succ('导入失败：' + r, 1500)
							}
							modal.close()
						},
						error : function() {
							modalService.error('导入失败', 1500)
						}
					});
				},
				term : ctrl.termQueryParam,
				supCtrl : ctrl
			}, {
				backdrop : false
			})
		}
		var L = {}
		L.onQuerySuccess = function(r) {
			var ids = []
			angular.forEach(r.content, function(v) {
				ids.push(v.stuInfo.id)
			})
			$http.post(':root/course/getStuApplyLeaveCount', {
				stuId : ids
			}).success(function(R) {
				CtrlUtil.addProperty(r.content, R, 'applyCount',undefined,'stuInfo.id')
			})
		}
		CtrlUtil.build(L, ':root/course/queryLeaves', ctrl)
		L.audit = function(item) {
			var content = $element.find('[template-leave]>[audit]').clone();
			item.pass = true
			modalService.confirm({
				content : content,
				result : item
			}).then(function(r) {
				$http.post(':root/course/auditLeave', {
					id : r.id,
					pass : r.pass
				}).success(function(R) {
					if (R) {
						modalService.succ('审批成功', 1500)
						L.query()
					}

				})
			})
		}
		L.detail = function(item) {
			var content = $element.find('[template-leave]>[detail]').clone();
			modalService.show({
				content : content,
				result : item

			})
		}
		L.toImageUrl = CtrlUtil.toImageUrl
		L.batch = function() {
			var content = $element.find('[template-leave]>[batchaudit]').clone();
			var id = CtrlUtil.getIds(CtrlUtil.getChecked(L))
			modalService.confirm({
				content : content,
				result : {
					id : id,
					pass : true
				}
			}).then(function(r) {
				$http.post(':root/course/batchAuditLeave', r).success(function(R) {
					if (R) {
						modalService.succ('审批成功', 1500)
						L.query()
					}

				})
			})
		}
		L.init = function() {

		}
		var D = {}
		D.build = function() {
			var p = CtrlUtil.getQueryParams(ctrl);
			$http.post(':root/course/generateAttendance', p).success(function(r) {
				ctrl.query()
				modalService.succ('生成成功', 1500)
			})
		}
		D.updateStatus = function(item) {
			item.updating = true
			$http.post(':root/course/updateAttendance', item)['finally'](function() {
				item.updating = false
			})
		}
		CtrlUtil.build(D, ':root/course/queryAttendance', ctrl)
		this.Q = Q
		this.L = L
		this.D = D
		this.query = function() {
			$scope.ctrl && $scope.ctrl.query()
		}
		this.changeCtrl = function(n) {
			$scope.ctrl = ctrl[n]
			ctrl.query()
		}
		$http.post(':root/term/queryTerms').success(function(r) {
			ctrl.terms = r.content
			ctrl.termQueryParam = ctrl.terms.length > 0 ? ctrl.terms[0].id : ''
			ctrl.getClassInfo(ctrl.termQueryParam)
		})
		CtrlUtil.loadDepartment(ctrl)
		this.getClassInfo = function(term, target) {
			target = target || ctrl
			return $http.post(':root/trainee/getClassInfoList', {
				term : term
			}).success(function(r) {
				target.classInfoList = r
			})
		}
		this.reset = function() {
			var before = ctrl.courseQueryParam
			CtrlUtil.resetQueryParams(ctrl)
			if ($scope.ctrl != ctrl.Q) {
				ctrl.courseQueryParam = before
			}
		}
		this['import'] = function() {

		}
	}
</script>

<div ng-controller="AttenDanceManagerCtrl as supCtrl" ng-init="supCtrl.changeCtrl('Q');">
	<!-- Search area -->
	<div class="panel panel-cys">
		<div class="panel-heading">
			<h3 class="panel-title">
				<span class="glyphicon glyphicon-search"></span>
				信息查询
			</h3>
		</div>
		<div class="panel-body">
			<div class="container-fluid">
				<form class="form-horizontal">
					<div ng-if="ctrl==supCtrl.Q">
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<select class="form-control" ng-change="supCtrl.getClassInfo(m.id)" chosen
									ng-options="m.id as (m.name+'-第'+m.qs+'期') for m in supCtrl.terms" ng-model="supCtrl.termQueryParam">
									<option value="">期次</option>
								</select>
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<input placeholder="课程名称" ng-model="supCtrl.nameQueryParam" type="text" class="form-control">
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<select chosen ng-model="supCtrl.typeQueryParam" class="form-control">
									<option value="">课程类别</option>
									<option value="1">必修课</option>
									<option value="2">选修课</option>
									<option value="3">体验课</option>
								</select>
							</div>
						</div>
					</div>
					<div ng-if="ctrl==supCtrl.L">
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<span style="overflow: hidden;" ng-bind="supCtrl.currentCourse.term.name" title="{{supCtrl.currentCourse.term.name}}"
									class="form-control"></span>
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<span placeholder="课程名称" ng-bind="supCtrl.currentCourse.name" type="text" class="form-control"></span>
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<span ng-bind="supCtrl.currentCourse.kclb|courseType" class="form-control"></span>
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<input placeholder="学号/姓名" ng-model="supCtrl.stuNameQueryParam" type="text" class="form-control">
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<select class="form-control" chosen ng-change="supCtrl.changeDepartment()" ng-options="m.id as m.name for m in supCtrl.departments"
									ng-model="supCtrl.departmentQueryParam">
									<option value="">院系</option>
								</select>

							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<select class="form-control" chosen ng-options="m.id as m.bjName for m in supCtrl.classInfoList" ng-model="supCtrl.clazzQueryParam">
									<option value="">班级</option>
								</select>
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<select chosen ng-model="supCtrl.statusQueryParam" class="form-control">
									<option value="">审核状态</option>
									<option ng-if="$user.type==1" value="1">待审核</option>
									<option ng-if="$user.type>1" value="2">待审核</option>
									<option ng-if="$user.type==1" value="2">待学校审核</option>
									<option value="3">审核通过</option>
									<option ng-if="$user.type>1" value="-1">请求驳回</option>
								</select>
							</div>
						</div>
					</div>
					<div ng-if="ctrl==supCtrl.D">
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<span style="overflow: hidden;" ng-bind="supCtrl.currentCourse.term.name" title="{{supCtrl.currentCourse.term.name}}"
									class="form-control"></span>
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<span title="{{supCtrl.currentCourse.name}}" ng-bind="supCtrl.currentCourse.name" type="text" class="form-control"></span>
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<span ng-bind="supCtrl.currentCourse.kclb|courseType" class="form-control"></span>
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<input placeholder="学号/姓名" ng-model="supCtrl.stuNameQueryParam" type="text" class="form-control">
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<select class="form-control" chosen ng-change="supCtrl.changeDepartment()" ng-options="m.id as m.name for m in supCtrl.departments"
									ng-model="supCtrl.departmentQueryParam">
									<option value="">院系</option>
								</select>

							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<select class="form-control" chosen ng-options="m.id as m.bjName for m in supCtrl.classInfoList" ng-model="supCtrl.clazzQueryParam">
									<option value="">班级</option>
								</select>
							</div>
						</div>
						<div class="form-group col-md-4 remove-margin-horizontal">
							<div class="col-md-10 col-md-offset-1">
								<select class="form-control" chosen ng-model="supCtrl.statusQueryParam">
									<option value="">考勤状态</option>
									<option value="0">正常</option>
									<option value="1">请假</option>
									<option value="2">缺勤</option>
								</select>
							</div>
						</div>
					</div>
				</form>
				<div class="row pull-right">
					<div class="col-md-12">
						<button ng-click="supCtrl.query()" type="submit" class="btn btn-cys btn-sm">
							<span class="glyphicon glyphicon-search"></span>
							查询
						</button>
						<button ng-click="supCtrl.reset()" type="reset" class="btn btn-default btn-sm">
							<span class="glyphicon glyphicon-repeat"></span>
							重置
						</button>
					</div>
				</div>
			</div>
		</div>

	</div>
	<!-- Search area END -->
	<div ng-if="ctrl==supCtrl.L" ng-include="':root/components/manager/leave.html'"></div>
	<div ng-if="ctrl==supCtrl.D" ng-include="':root/components/manager/checking-in.html'"></div>
	<div ng-if="ctrl==supCtrl.Q">
		<!-- operation area -->
		<a type="button" class="btn btn-cys btn-sm" ng-click="ctrl.import()">
			<span class="glyphicon glyphicon-import"></span>
			考勤导入
		</a>
		<!-- operation area -->
		<div class="pull-right" ng-include="':root/components/page.html'"></div>
		<!-- operation area END -->
		<!-- data grid area -->
		<div style="padding-top: 10px;">
			<table class="table table-bordered table-hover table-striped">
				<tr>
					<th>
						<input ng-change="ctrl.checkAll(ctrl.$checkAll)" ng-model="ctrl.$checkAll" type="checkbox" />
					</th>
					<th>课程名称</th>
					<th>课程类型</th>
					<th>开课时间</th>
					<th>开课状态</th>
					<th>已报名人数</th>
					<th>考勤人数</th>
					<th>请假人数</th>
					<th>缺勤人数</th>
					<th>操作</th>
				</tr>
				<tr ng-repeat="item in ctrl.entities.content">
					<td>
						<input ng-model="item.$checked" type="checkbox" />
					</td>
					<td>{{item.name}}</td>
					<td>{{item.kclb|courseType}}</td>
					<td>{{item.kcsj|date:'yyyy-MM-dd HH:mm'}}</td>
					<td>{{item.statusString}}</td>
					<td>{{item.applyCount||0}}</td>
					<td>{{item.checking.attend|nullText}}</td>
					<td>{{item.checking.leave|nullText}}</td>
					<td>{{item.checking.absence|nullText}}</td>
					<td>
						<a href="javascript:;" ng-click="ctrl.detail(item)">考勤明细</a>
						<a href="javascript:;" ng-click="ctrl.audit(item)">请假审核</a>
					</td>
				</tr>
			</table>
			<p>
				共有记录：
				<span>{{ctrl.entities.totalElements}}</span>
				条
			</p>
		</div>
	</div>
	<!-- data grid area END -->
	<div ng-non-bindable template class="hidden">
		<div import class="col-md-10 col-md-offset-1">
			<form class="form-horizontal" enctype="multipart/form-data" method="post">
				<div class="form-group">
					<div class="col-md-12 ">
						<select name="term" class="form-control" chosen ng-value="term">
							<option value="">期次</option>
							<option value="{{m.id}}" ng-selected="$index==0" ng-repeat="m in supCtrl.terms">{{(m.name+'-第'+m.qs+'期')}}</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<div class="col-md-12">
						<input class="form-control" id="fileImport" name="file" type="file" />
					</div>
				</div>
				<div class="form-group">
					<div class="col-md-12">
						<a type="submit" ng-click="upload()" class="btn btn-success">导入</a>
						<a class="btn btn-link btn-sm" href="asset/excel/attendanceImportSample.xls">
							<span class="glyphicon glyphicon-arrow-down"></span>
							导入模板下载
						</a>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>